const Router = require('koa-router')
const {
	list,
	items,
	explist,
	sourcelist,
	exportExcel,
} = require('../modules/module-match_songlist')

const songsRouter = new Router()

// songsRouter.use((ctx, next) => {
// 	next()
// })

songsRouter.post('/list', async (ctx, next) => {
	let params = ctx.request.body
	let result = await list(params)
	ctx.response.body = result
})

songsRouter.post('/items', async (ctx, next) => {
	let params = ctx.request.body
	let result = await items(params)
	ctx.response.body = result
})

songsRouter.post('/sourcelist', async (ctx, next) => {
	let result = await sourcelist()
	ctx.response.body = result
})

songsRouter.post('/explist', async (ctx, next) => {
	let params = ctx.request.body
	let result = await explist(params)
	ctx.response.body = result
})

songsRouter.get('/excel', async (ctx, next) => {
	let params = ctx.request.query
	let result = await exportExcel(params)
	// ctx.res.setHeader('Content-Type', 'application/vnd.openxmlformats')
	// ctx.res.setHeader('Content-Disposition', 'attachment; filename=excel.xlsx')
	// ctx.res.end(result, 'binary')
	ctx.response.set('Content-Type', 'application/vnd.openxmlformats')
	ctx.response.set('Content-Disposition', `attachment; filename=${result.filename}`)
	ctx.response.body = result.body
})

module.exports = songsRouter